package com.flower8.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description: 时间转换工具类
 * @Author: tanglin
 * @Date: 2019/10/14 11:35
 * @Version: 1.0
 */
public class DateUtils {
    public static final String FORMATTER_MINUTE = "yyyy-MM-dd HH:mm";
    public static final String FORMATTER_SECOND = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMATTER_MILLISECOND = "yyyy-MM-dd HH:mm:ss.SSS";

    /**
     * 时间转换为带时分秒的格式
     * @param date
     * @return
     */
    public static String formatWithSecond(String date) {
        String result = "";
        try{
            if (date != null) {
                SimpleDateFormat fmt  = new SimpleDateFormat(FORMATTER_SECOND);
                Date time = fmt.parse(date);//将数据库出的 timestamp 类型的时间转换为java的Date类型
                result = fmt.format(time);;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    public static String formatDate(Date date){
        String result = "";
        SimpleDateFormat fmt  = new SimpleDateFormat(FORMATTER_SECOND);
        result = fmt.format(date);
        return result;
    }
    /**
     * 字符串转换成日期
     * @param str
     * @return date
     */
    public static Date StrToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat(FORMATTER_SECOND);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

	/**
	 * 
	 * @return {@link java.sql.Date}
	 */
	public static java.sql.Date getToday() {
		return new java.sql.Date(System.currentTimeMillis());
	}

    /**
     * 
     * @return {@link java.util.Timestamp}
     */
    public static java.sql.Timestamp getCurrentTimestamp() {
        return new Timestamp(System.currentTimeMillis());
    }

	/**
	 * 
	 * @param date
	 * @param minute
	 * @return {@link java.sql.Timestamp}
	 */
	public static java.sql.Date addDay(java.sql.Date date, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(date.getTime());
		calendar.add(Calendar.DAY_OF_MONTH, day);
		return new java.sql.Date(calendar.getTimeInMillis());
	}

    /**
     * 
     * @param timestamp
     * @param second
     * @return {@link java.sql.Timestamp}
     */
    public static java.sql.Timestamp addSecond(java.sql.Timestamp timestamp, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp.getTime());
        calendar.add(Calendar.SECOND, second);
        return new java.sql.Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 
     * @param timestamp
     * @param minute
     * @return {@link java.sql.Timestamp}
     */
    public static java.sql.Timestamp addMinute(java.sql.Timestamp timestamp, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp.getTime());
        calendar.add(Calendar.MINUTE, minute);
        return new java.sql.Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 
     * @param timestamp
     * @param hour
     * @return {@link java.sql.Timestamp}
     */
    public static java.sql.Timestamp addHour(java.sql.Timestamp timestamp, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp.getTime());
        calendar.add(Calendar.HOUR_OF_DAY, hour);
        return new java.sql.Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 
     * @param timestamp
     * @param day
     * @return {@link java.sql.Timestamp}
     */
    public static java.sql.Timestamp addDay(java.sql.Timestamp timestamp, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp.getTime());
        calendar.add(Calendar.DAY_OF_MONTH, day);
        return new java.sql.Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 
     * @param timestamp
     * @param day
     * @return {@link java.sql.Timestamp}
     */
    public static java.sql.Timestamp addMonth(java.sql.Timestamp timestamp, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp.getTime());
        calendar.add(Calendar.MONTH, month);
        return new java.sql.Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 
     * @param timestamp
     * @param day
     * @return {@link java.sql.Timestamp}
     */
    public static java.sql.Timestamp addYear(java.sql.Timestamp timestamp, int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp.getTime());
        calendar.add(Calendar.YEAR, year);
        return new java.sql.Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 日期格式化
     * 
     * @param dateFormat(日期格式，例如：yyyy-MM-dd HH:mm:ss)
     * @param timestamp
     * @return
     */
    public static String toStringDate(String format, java.util.Date timestamp) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(timestamp);
    }

    /**
     * 日期格式化
     * 
     * @param dateFormat(日期格式，例如：yyyy-MM-dd HH:mm:ss)
     * @param timestamp(long型)
     * @return
     */
    public static String toStringDate(String format, Long timestamp) {
        try {
            Timestamp timestampT = new Timestamp(timestamp);
            return toStringDate(format, timestampT);
        } catch (Exception e) {
            return String.valueOf(timestamp);
        }
    }

    /**
     * 
     * @param utilDate {@link java.util.Date}
     * @return {@link java.sql.Date}
     */
    public static java.sql.Date toSqlDate(java.util.Date utilDate) {
        return new java.sql.Date(utilDate.getTime());
    }

    /**
     * <pre>
     * eg:("2015", "05", "13"). return 2014-05-15 
     * eg:("2015", "3", "4"). return 2014-03-04
     * </pre>
     * 
     * @param year String
     * @param month String
     * @param day String
     * @return {@link java.sql.Date}
     */
    public static java.sql.Date toSqlDate(String year, String month, String day) {
        return toSqlDate(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(day));
    }

    /**
     * <pre>
     * eg:(2015, 11, 13). return 2014-11-13
     * eg:(2015, 3, 4). return 2014-03-04
     * </pre>
     * 
     * @param year int
     * @param month int
     * @param day int
     * @return {@link java.sql.Date}
     */
    public static java.sql.Date toSqlDate(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day, 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return toSqlDate(calendar.getTime());
    }

    /**
     * 日期格式化
     * 
     * @param dateFormat(日期格式，例如：yyyy-MM-dd HH:mm:ss)
     * @param timestamp(String型)
     * @return
     */
    public static String toStringDate(String format, String timestamp) {
        try {
            Timestamp timestampT = Timestamp.valueOf(timestamp);
            return toStringDate(format, timestampT);
        } catch (Exception e) {
            return timestamp;
        }
    }
}
